package com.example.itcacp.repository.mybatis;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.itcacp.entity.Tag;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 标签Mapper层
 * */

@Repository
public interface TagMapper extends BaseMapper<Tag> {


    @SelectProvider(type = TagSqlProvider.class, method = "selectTagAll")
    @Results({
            @Result(property = "tagId", column = "tag_id"),
            @Result(property = "tagName", column = "tag_name"),
            @Result(property = "created_at", column = "created_at"),
            @Result(property = "updated_at", column = "updated_at"),
    })
    List<Tag> selectTagAll(String tagName);

    class TagSqlProvider {
        public String selectTagAll(String tagName) {
            return new SQL () {{
                SELECT ( "*" );
                FROM ( "tc_tag" );
                boolean isWHERE = false; // categoryName为空时不添加WHERE语句
                if (tagName != null && !tagName.isEmpty ()) {
                    WHERE ( "tag_name LIKE CONCAT('%', #{tagName}, '%')" );
                    isWHERE = true;
                }
            }}.toString ();
        }
    }
}
